// pages/member/settings/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: '',
    wechatNumber: '',
    nickName: '',
    imgList: [], // 图片列表
    imgbacklist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let open_id = wx.getStorageSync('user_new')
    console.log(open_id)
    app.core('get', 'userInfo?open_id=' + open_id, '', (res) => {
      console.log(res)
      let user = res.data
      this.setData({
        phoneNumber: user.phone,
        wechatNumber: user.wechatId,
        nickName: user.nickname,
        // imgList: [user.headpic]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  nickname(e) {
    let nickName = e.detail.value
    this.setData({
      nickName
    })
  },
  phonenumber(e) {
    let phoneNumber = e.detail.value
    this.setData({
      phoneNumber
    })
  },
  wechatnumber(e) {
    let wechatNumber = e.detail.value
    this.setData({
      wechatNumber
    })
  },
  uploadimg(imglist) {
    var that = this;
    var photo_url = that.data.imgList;
    console.log(photo_url)
    wx.showLoading({
      title: '上传中',
    });
    wx.uploadFile({
      url: 'https://xkcloude.hfhouse.com/routineclient/upload_file',
      filePath: photo_url[0],
      name: 'photo_url',
      success(res) {
        var x = JSON.parse(res.data);
        console.log(x)
        if (x.resultCode == 500) {
          wx.showToast({
            title: x.data.msg,
            icon: 'none',
            duration: 3000
          })
          return
        }else if(x.resultCode == 200) {
          wx.showToast({
            title: '上传成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            imgbacklist: x.data.album
          })
        }
        wx.hideLoading()      
      }
    })
  },
  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        // console.log(res)
        this.setData({
          imgList: res.tempFilePaths
        })
        this.uploadimg(res.tempFilePaths)
        // console.log(this.data.imgList)
        // if (this.data.imgList.length != 0) {
        //   this.setData({
        //     imgList: this.data.imgList.concat(res.tempFilePaths)
        //   })
        //   //this.uploadimg(this.data.imgList.concat(res.tempFilePaths))
        //   this.uploadimg(this.data.imgList);
        // } else {
        //   this.setData({
        //     imgList: res.tempFilePaths
        //   })
        //   this.uploadimg(res.tempFilePaths)
        // }
      }
    });


  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '提醒',
      content: '确定要删除这张照片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: []
          })
        }
        //   if (this.data.imgList.length == 0) {
        //     wx.clearStorageSync('imgarr');
        //     return;
        //   }

        //   this.uploadimg(this.data.imgList)
        // }
        // wx.setStorageSync('imgarr', this.data.imgList )
      }
    })
  },
  savesetting() {
    let nickname = this.data.nickName
    let phone = this.data.phoneNumber
    let wechatId = this.data.wechatNumber
    let headpic = this.data.imgbacklist[0]
    let open_id = wx.getStorageSync('user_new');
    console.log(nickname, phone, wechatId, headpic, open_id)

    // if (title == "") {
    //   wx.showToast({
    //     title: '请输入标题',
    //     icon: 'none',
    //     duration: 2000
    //   })
    // }
    // else if (content == "") {
    //   wx.showToast({
    //     title: '请输入内容',
    //     icon: 'none',
    //     duration: 2000
    //   })
    // }
    // else {
    wx.showLoading({
      title: '保存中',
    })
    app.core('post', "profile", {
        open_id,
        nickname,
        phone,
        wechatId,
        headpic
      }, (res => {
        console.log(res);
        if (res.resultCode == 200) {
          wx.hideLoading()
          // wx.setStorageSync('userInfoChange', res.data)
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/member/index',
            })
          }, 1000)
        } else {
          wx.showToast({
            title: '修改失败',
            icon: 'none',
            duration: 3000
          })
        }
      }),
      (err => {
        wx.showToast({
          title: '网络错误',
          icon: 'none',
          duration: 1000
        })
        wx.hideLoading()
      }))
    // }

  }, // 发布写一写
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})